﻿@using Citi.Scheduler.Domain
@model List<Citi.Scheduler.WebUi.Models.RoutineExecutionHistoryModel>
@{
    ViewBag.Title = "Execution History";
}
@section Menu
{
    @Html.Partial("~/Views/Menu/Execution.cshtml", new ViewDataDictionary())
}
@if (!Model.Any() || Model.First().Id == 0)
{
    <text>No data was found</text>
    return;
}
<h1>
    Execution History - @Model.First().RoutineName</h1>
@{
    var last = Model.Last();

    var callout = last.Status == RoutineExecutionStatus.Error || last.Status == RoutineExecutionStatus.PredecessorRoutineWithError
        || last.Status == RoutineExecutionStatus.Stopped || last.Status == RoutineExecutionStatus.Canceled || last.Status == RoutineExecutionStatus.CanceledAfterWaiting
        ? "danger"
        : last.Status == RoutineExecutionStatus.Executed
            ? "green"
            : last.Status == RoutineExecutionStatus.InProgress
                ? "info"
                : "neutral";
}
<div class="bs-callout bs-callout-@callout" style="width: 60%">
    <p class="title">
        <b>Current Status</b></p>
    <p>
        @{
            <b>Date:</b>  @Html.DisplayFor(modelItem => last.Date)
            <br />
            <b>Status:</b> @last.Status.GetDescription()
            <br />
            <b>Description:</b>   @Html.DisplayFor(modelItem => last.Details)
            if (last.MachineHostName != null)
            {
            <br />
            <b>Machine:</b>   @Html.DisplayFor(modelItem => last.MachineHostName)
            }
        }
    </p>
</div>
<table id="histories" class="table table-bordered table-striped">
    <tr>
        <th>
            Date
        </th>
        <th>
            Details
        </th>
        <th>
            Status
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Date)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Details)
            </td>
            <td>
                @item.Status.GetDescription()
            </td>
        </tr>
    }
    @if (Model.Last().Status != RoutineExecutionStatus.InProgress && Model.Last().Status != RoutineExecutionStatus.PredecessorNotScheduled
    && Model.Last().Status != RoutineExecutionStatus.Scheduled && Model.Last().Status != RoutineExecutionStatus.WaitingFreeMachine
    && Model.Last().Status != RoutineExecutionStatus.WaitingPredecessorRoutine && Model.Last().Status != RoutineExecutionStatus.WaitingSpecificFreeMachine)
    {
        <input type="hidden" id="hiddenFinished" value="true" />
    }
    @if (ViewBag.ShowErrors && Model.Any())
    {
        <div>@Html.Action("ByRoutineExecution", "RoutineError", new { routineExecutionId = Model.First().RoutineExecutionId })</div>
    }
</table>
@{
    <span>@string.Format("Total minutes to execute: {0}", Math.Round((Model.Last().Date - Model.First().Date).TotalMinutes, 2))</span>
    <br />
    if (Model.Last().Status != RoutineExecutionStatus.InProgress && Model.Last().Status != RoutineExecutionStatus.PredecessorNotScheduled
        && Model.Last().Status != RoutineExecutionStatus.Scheduled && Model.Last().Status != RoutineExecutionStatus.WaitingFreeMachine
        && Model.Last().Status != RoutineExecutionStatus.WaitingPredecessorRoutine && Model.Last().Status != RoutineExecutionStatus.WaitingSpecificFreeMachine)
    {
        var history = Model.FirstOrDefault(x => x.Status == RoutineExecutionStatus.WaitingFreeMachine || x.Status == RoutineExecutionStatus.WaitingPredecessorRoutine
            || x.Status == RoutineExecutionStatus.WaitingSpecificFreeMachine);

        if (history != null)
        {
    <span>@string.Format("Minutes waiting for a free machine: {0}", Math.Round((Model[Model.IndexOf(history) + 1].Date - history.Date).TotalMinutes, 2))</span>
        }
    }
}
